package jp.co.sharp.android.parents.kidsguard.utils;

import android.app.Service;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import jp.co.sharp.android.parents.kidsguard.R;

public class CommonUtils {

    private static MediaPlayer mMediaPlayer;
    private static Vibrator mVibrator;

    /**
     * 保存bitmap到SD卡
     * @param bitName 保存的名字
     * @param mBitmap 图片对像
     * return 生成压缩图片后的图片路径
     */
    public static String saveMyBitmap(String bitName, Bitmap mBitmap) {
        File f = new File(bitName );
        try {
            f.createNewFile();
        } catch (IOException e) {
            System.out.println("在保存图片时出错：" + e.toString());
        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        } catch (Exception e) {
            return "create_bitmap_error";
        }
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitName;
    }

    /**
     * Portrait ID --> Portrait Resource Code
     * @param pPortrait Portrait ID
     * @return Resource Code
     */
    public static int chooseImage(int pPortrait) {
        switch (pPortrait){
            case 1 :
                return R.mipmap.ic_adrbook_rhino;
            case 2 :
                return R.mipmap.ic_adrbook_girafe;
            case 3 :
                return R.mipmap.ic_adrbook_bird;
            case 4 :
                return R.mipmap.ic_adrbook_dolphin;
            case 5 :
                return R.mipmap.ic_adrbook_elephant;
            case 6 :
                return R.mipmap.ic_adrbook_car;
            case 7 :
                return R.mipmap.ic_adbook_train;
            case 8 :
                return R.mipmap.ic_adrbook_plane;
            case 9 :
                return R.mipmap.ic_adrbook_ship;
            case 10 :
                return R.mipmap.ic_adrbook_bike;
            case 11 :
                return R.mipmap.ic_adrbook_sweet;
            case 12 :
                return R.mipmap.ic_adrbook_icecream;
            case 13 :
                return R.mipmap.ic_adrbook_candy;
            case 14 :
                return R.mipmap.ic_adrbook_chocolate;
            case 15 :
                return R.mipmap.ic_adbook_cupcake;
            case 16 :
                return R.mipmap.ic_adrbook_male1;
            case 17 :
                return R.mipmap.ic_adrbook_female1;
            case 18 :
                return R.mipmap.ic_adrbook_male2;
            case 19 :
                return R.mipmap.ic_adrbook_female2;
            case 20 :
                return R.mipmap.ic_adrbook_male3;
            case 21 :
                return R.mipmap.ic_adrbook_female3;
            case 22 :
                return R.mipmap.ic_adrbook_male4;
            case 23 :
                return R.mipmap.ic_adrbook_female4;
            case 24 :
                return R.mipmap.ic_adrbook_home;
            case 25 :
                return R.mipmap.ic_adrbook_school;
            case 26 :
                return R.mipmap.ic_adrbook_office;
            default:
                return R.mipmap.ic_adrbook_default;
        }
    }

    /**
     *
     * @param pContext
     * @param pRingtoneUri
     */
    public static void playRingtone(Context pContext, String pRingtoneUri) {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
        }
        Uri ringtoneUri;
        if(null == pRingtoneUri || pRingtoneUri.equals("")) {
            ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        } else {
            ringtoneUri = Uri.parse(pRingtoneUri);
        }
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.reset();
        try {
            mMediaPlayer.setDataSource(pContext, ringtoneUri);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param pContext
     * @param pRingtoneUri
     */
    public static void stopRingtone(Context pContext, String pRingtoneUri) {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    /**
     *
     * @param pContext
     */
    public static void playVibrator(Context pContext) {
        mVibrator = (Vibrator)pContext.getSystemService(Service.VIBRATOR_SERVICE);
        mVibrator.vibrate(new long[]{500, 500}, 0);
    }

    /**
     *
     * @param pContext
     */
    public static void stopVibrator(Context pContext) {
        if (mVibrator == null) {
            return;
        }
        mVibrator.cancel();
    }
}
